<?php
namespace app\im\model;

use app\BaseModel;

class ImMyReply extends BaseModel
{
    //定义表名称
    protected $name = 'longbing_card_quick_reply';
    
    public function searchContentAttr($query, $value, $data)
    {
        $query->where('content','like', '%' . $value . '%');
    }
    public function searchUserIdAttr($query, $value, $data)
    {
        $query->where('user_id','=', $value);
    }
    public function searchUniacidAttr($query, $value, $data)
    {
        $query->where('uniacid','=', $value);
    }
    public function type()
    {
        return $this->hasOne('ImReplyType' ,'id' ,'type');
    }
    //创建话术
    public function createReply($data)
    {
        $data['create_time'] = time();
        $result =  $this->save($data);
        return !empty($result);
    }
    
    //更新话术
    public function updateReply($filter ,$data)
    {
        $data['update_time'] = time();
        $result = $this->where($filter)->update($data);
        return !empty($result);
    }
    
    //获取话术列表
    public function listReply($filter)
    {
        $result = $this->where($filter)
            ->select();
        if(!empty($result)) $result = $result->toArray(); 
        return $result;
    } 
    //获取话术列表(分页)
    public function listReplys($filter ,$page_config)
    {
        $result = $this->with(['type'])
                       ->withSearch(['content' ,'user_id' ,'uniacid'] ,$filter)
                       ->order('top' ,'desc')
                       ->page($page_config['page'] ,$page_config['page_count'])
                       ->select();
        if(!empty($result)) $result = $result->toArray(); 
        return $result;
    }        
    //获取话术总数
    public function getReplyCount($filter)
    {
        $result = $this->withSearch(['content' ,'user_id' ,'uniacid'] ,$filter)->count();
        if(empty($result)) $result = 0;
        return $result;
    }
    
    //删除话术
    public function deleteReply($filter)
    {
        return $this->destoryReply($filter);
    }
    
    //删除（真删除）
    public function destoryReply($filter)
    {
        $result = $this->where($filter)->delete();
        return !empty($result);
    }    
}